/*
 * @Copyright (c) 2018 缪聪(mcg-helper@qq.com)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");  
 * you may not use this file except in compliance with the License.  
 * You may obtain a copy of the License at  
 *     
 *     http://www.apache.org/licenses/LICENSE-2.0  
 *     
 * Unless required by applicable law or agreed to in writing, software  
 * distributed under the License is distributed on an "AS IS" BASIS,  
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
 * See the License for the specific language governing permissions and  
 * limitations under the License.
 */

package com.mcgrobot.listener;

import java.net.InetAddress;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.web.context.WebServerApplicationContext;
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.boot.web.server.WebServer;
import org.springframework.context.ApplicationListener;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

/**
 * 
 * @ClassName:   McgHelperRunComplete   
 * @Description: TODO(系统成功启动完成后的操作) 
 * @author:      缪聪(mcg-helper@qq.com)
 * @date:        2018年3月9日 下午5:30:15  
 *
 */
@Component
public class McgHelperRunComplete implements ApplicationListener<WebServerInitializedEvent> {

    private static Logger logger = LoggerFactory.getLogger(McgHelperRunComplete.class);

    @Override
    public void onApplicationEvent(WebServerInitializedEvent event) {
    	try {
	        WebServer server = event.getWebServer();
	        WebServerApplicationContext context = event.getApplicationContext();
	        Environment env = context.getEnvironment();
	        String ip = InetAddress.getLocalHost().getHostAddress();
	        int port = server.getPort();
	        String contextPath = env.getProperty("server.servlet.context-path");
	        if (contextPath == null) {
	            contextPath = "";
	        }
	        
	        logger.info("--------------------------------------------------------------------------------");
	        logger.info("微克机器人启动完成!");
	        logger.info("请访问：http://{}:{}{}", ip, port, contextPath);
	        logger.info("--------------------------------------------------------------------------------");
	    } catch (Exception e) {
	    	logger.error("输出访问信息出错：", e);
		}
    }
    
}
